package net.alpenviewer.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ProvidesResize;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.Widget;


public class CenterTopPanel extends ResizeComposite implements ProvidesResize {

	interface Binder extends UiBinder<Widget, CenterTopPanel> { }
	private static final Binder binder = GWT.create(Binder.class);

	@UiField LayoutPanel map;

	final MapWidget mapWidget = MapModel.getInstance().getMap();	

	public CenterTopPanel() {
		initWidget(binder.createAndBindUi(this));
		
		// Add the MapWidget with initial size (deferred for getting correct container size)
		DeferredCommand.addCommand(new Command() {			
			@Override public void execute() {				
				mapWidget.setWidth(map.getOffsetWidth()+"px");
				mapWidget.setHeight(map.getOffsetHeight()+"px");
				map.add(mapWidget);				
			}
		});

	}

	@Override public void onResize() {			
		super.onResize();
		/* this will let map automatically resized to its container. */
		LatLng center = mapWidget.getCenter();
		mapWidget.setSize("100%", "100%");
		mapWidget.setCenter(center);
	}

}
